import os
import yaml
from pathlib import Path

# 项目根目录
ROOT_DIR = Path(__file__).resolve().parents[2]
CONFIG_FILE = ROOT_DIR / 'config' / 'api_config.yaml'

def load_config():
    """加载YAML配置文件"""
    try:
        if not CONFIG_FILE.exists():
            # 如果配置文件不存在，创建默认配置
            default_config = {
                'api': {
                    'base_url': 'https://api.example.com',
                    'timeout': 30
                },
                'auth': {
                    'login_endpoint': '/auth/login',
                    'logout_endpoint': '/auth/logout',
                    'token_header': 'Authorization',
                    'token_prefix': 'Bearer'
                },
                'storage': {
                    'auth_dir': 'auto',
                    'auth_file': 'auto.json'
                }
            }
            
            # 确保config目录存在
            config_dir = CONFIG_FILE.parent
            if not config_dir.exists():
                config_dir.mkdir(parents=True, exist_ok=True)
                
            # 写入默认配置
            with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
                yaml.dump(default_config, f, default_flow_style=False)
            
            return default_config
        
        # 读取配置文件
        with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
            return yaml.safe_load(f)
    except Exception as e:
        print(f"加载配置文件失败: {e}")
        # 返回默认配置
        return {
            'api': {
                'base_url': 'https://api.example.com',
                'timeout': 30
            },
            'auth': {
                'login_endpoint': '/auth/login',
                'logout_endpoint': '/auth/logout',
                'token_header': 'Authorization',
                'token_prefix': 'Bearer'
            },
            'storage': {
                'auth_dir': 'auto',
                'auth_file': 'auto.json'
            }
        }

# 加载配置
config = load_config() 